﻿using System;

class MaxSumOfElementsK

            // Write a program that reads two integer numbers N and K and an array of N elements from the console. Find in the array those K elements that have maximal sum.

{
    static void Main()
    {
        Console.Write("Enter the length (N) of the array: ");
        int arraySize = int.Parse(Console.ReadLine());
        Console.Write("Enter the value for K such that 1<K<N: ");
        int k = int.Parse(Console.ReadLine());
        int[] array = new int[arraySize];
        Console.WriteLine();

        Console.WriteLine("Enter the elements of the array");
        for (int i = 0; i < arraySize; i++)
        {
            array[i] = int.Parse(Console.ReadLine());
        }

        Array.Sort(array);

        long sum = 0;
        arraySize--;
        while (k > 0)
        {
            sum += array[arraySize];
            arraySize--;
            k--;
        }
        Console.WriteLine("The maximal sum of 'K' elements is: {0}", sum);
    }
}

